import { ipcMain } from 'electron';

const keyNegotiationUserSet = new Set<string>();

ipcMain.on('ipc:key-negotiation-status', (event, status) => {
  console.log('主进程已转发状态：', status);
  event.sender.send('ipc:key-negotiation-status', status);
});

ipcMain.on('ipc:user-no-online', (event, data) => {
  event.sender.send('ipc:user-no-online', data);
});

ipcMain.on('ipc:update-chat-window', (event, data) => {
  event.sender.send('ipc:update-chat-window', data);
});

ipcMain.on('ipc:add-key-negotiation-user', (event, friendId) => {
  console.log('已添加Set:', friendId);
  keyNegotiationUserSet.add(friendId);
  event.sender.send('ipc:add-key-negotiation-user', true);
});

// 移除Set
ipcMain.on('ipc:remove-key-negotiation-user', (event, friendId) => {
  keyNegotiationUserSet.delete(friendId);
  event.sender.send('ipc:remove-key-negotiation-user', true);
});

// 检查是否在Set中
ipcMain.on('ipc:check-key-negotiation-user', (event, friendId) => {
  event.sender.send(
    'ipc:check-key-negotiation-user',
    keyNegotiationUserSet.has(friendId)
  );
});

// 转发私聊消息
ipcMain.on(
  'ipc:private-chat-message',
  (event, senderId, timestamp, data, senderName) => {
    event.sender.send(
      'ipc:private-chat-message',
      senderId,
      timestamp,
      data,
      senderName
    );
  }
);

// 转发格式化后的聊天消息
ipcMain.on('ipc:formatted-chat-message', (event, res) => {
  event.sender.send('ipc:formatted-chat-message', res);
});

//转发解密后的推送的聊天消息
ipcMain.on(
  'ipc:put-socket-message',
  (
    event,
    friendId: string,
    friendName: string,
    time: number,
    message: string
  ) => {
    event.sender.send(
      'ipc:put-socket-message',
      friendId,
      friendName,
      time,
      message
    );
  }
);

ipcMain.on('ipc:refresh-friend-list', (event, _) => {
  event.sender.send('ipc:refresh-friend-list');
});

ipcMain.on('ipc:socket-init-success', (event, _) => {
  event.sender.send('ipc:socket-init-success');
});

ipcMain.on('ipc:client-online-status', (event, clientsOnline) => {
  event.sender.send('ipc:client-online-status', clientsOnline);
});

ipcMain.on('ipc:friend-online', (event, friendId, status) => {
  event.sender.send('ipc:friend-online', friendId, status);
});

ipcMain.on('ipc:close-chat-window', (event, _) => {
  event.sender.send('ipc:close-chat-window');
});

ipcMain.on(
  'ipc:change-submit-message-button',
  (event, senderId, friendId, status) => {
    event.sender.send(
      'ipc:change-submit-message-button',
      senderId,
      friendId,
      status
    );
  }
);
